package ae.pcfc.etks.online.itworx.ehs.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import ae.pcfc.etks.online.itworx.ehs.web.exception.WorkFlowException;
import ae.pcfc.etks.online.itworx.ehs.web.service.WorkflowService;
import ae.pcfc.etks.online.itworx.ehs.wf.service.WFService;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.K2WorklistItem;


public class WorkflowServiceImpl implements WorkflowService {

	private WFService wfService;

	public WFService getWfService() {
		return wfService;
	}

	public void setWfService(WFService wfService) {
		this.wfService = wfService;
	}

	public int createProcess(String processName, Map<String, String> workflowMap)
			throws WorkFlowException {
		try{
			return wfService.createProcess(processName, new ArrayList<String>(workflowMap.keySet()), new ArrayList<String>(workflowMap.values()));
		}catch (Exception e) {
			WorkFlowException workflowExp=new WorkFlowException(e);
			workflowExp.setErrorCode("error.workflow.unexpected");
			throw workflowExp;
		}
	}

	public boolean isOfficer(String officerName) throws WorkFlowException {
		try{
			return wfService.isOfficer(officerName);
		}catch (Exception e) {
			WorkFlowException workflowExp=new WorkFlowException(e);
			workflowExp.setErrorCode("error.workflow.unexpected");
			throw workflowExp;
		}
	}

	public boolean doAction(String doAction, String userName,
			String action, Map<String, String> workflowMap, boolean isOfficer) throws WorkFlowException {
		try{
			return wfService.doAction(doAction, userName, action,
				new ArrayList<String>(workflowMap.keySet()), new ArrayList<String>(workflowMap.values()), isOfficer);
		}catch (Exception e) {
			WorkFlowException workflowExp=new WorkFlowException(e);
			workflowExp.setErrorCode("error.workflow.unexpected");
			throw workflowExp;
		}
	}
	
	@Override
	public List<K2WorklistItem> getActiveTasks(String processName,
			String userName, boolean isOffecier, boolean isAssigned) throws WorkFlowException {
		try{
			return wfService.getActiveTasks(processName, userName, isOffecier, isAssigned);
		}catch (Exception e) {
			WorkFlowException workflowExp=new WorkFlowException(e);
			workflowExp.setErrorCode("error.workflow.unexpected");
			throw workflowExp;
		}
		
	}

	@Override
	public String[] getDelegatedUsers(String userName) throws WorkFlowException {
		try{
			return wfService.getDelegatedUsers(userName);
		}catch (Exception e) {
			WorkFlowException workflowExp=new WorkFlowException(e);
			workflowExp.setErrorCode("error.workflow.unexpected");
			throw workflowExp;
		}
	}
}
